home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static const char sccsid[] = "%Z%%I% %G% %U% %W%";
- #endif
- /*
- * COMPONENT_NAME: (RTTI) Run-Time Type System for C++
- *
- * FUNCTIONS:
- *
- * ORIGINS: 27
- *
- * (C) COPYRIGHT International Business Machines Corp. 1992
- * This work was supported by a grant from International Business
- * Machines, Inc. These procedures are contributed to the public domain
- * by International Business Machines Inc. "AS IS" without any warranty
- * of any kind including the warranty of merchantability or fitness
- * for a particular purpose.
- *
- *
- * This is free software. Feel free to redistribute and/or modify it.
- * Please send us e-mail and let us know if you have any problems
- * or suggestions.
- *
- * Arindam Banerji
- * axb@cse.nd.edu
- */
-
-
-
- // String.cc
- //
- // Implementation of string class - based on Stousroup's
- // implementation.
-
- #include <string.h>
- #include "String.h"
-
- #ifndef _KERNEL
- #include <iostream.h>
- #else
- #include <stdio.h>
- #endif // _KERNEL
-
- static char err_val = '\0' ;
-
- string::string()
- {
- p = new srep ;
- p->s = 0 ;
- } // the trivial constructor
-
- string::string ( const string &x )
- {
- x.p->n++ ; p = x.p ;
- } // increment the ref. count constructor
-
- string::string(const char *s )
- {
- p = new srep ;
- p->s = new char [strlen(s) + 1 ] ;
- strcpy ( p->s, s ) ;
- } // copy into new string
-
- string::operator char *()
- {
- char *z = new char [strlen (p->s) + 1];
- strcpy (z, p->s ) ;
- return (z) ;
- } // get a pointer to the string
-
- string::~string()
- {
- if ( --p->n == 0 )
- {
- delete [] p->s ;
- delete p ;
- } // delete is necessary
- } // destructor - reduce ref. count
-
- string& string::operator= (const char *s )
- {
- if ( p->n > 1 )
- {
- p->n-- ; p = new srep ;
- } // disconnect self
- else
- delete [] p->s ; // free the old string
-
- p->s = new char [strlen(s) + 1 ] ;
- strcpy ( p->s, s ) ;
- return ( *this) ;
- } // assignment operator - handles cleanup.
-
- string& string::operator= (const string &x)
- {
- x.p->n++ ; // make sure that x = x situation does not arise
- if (--p->n == 0 )
- {
- delete [] p->s ; delete p ;
- } // remove old representation
- p = x.p ; // set current rep. to point to that of the passed argument
- return ( *this ) ;
- } // assignment operator - smiliar to the constructor + handles cleanup
-
- #ifndef _KERNEL
- ostream& operator<< (ostream &s, const string &x )
- {
- return (s << x.p->s << " [ " << x.p->n << " ] " ) ;
- } // display the string
-
- istream& operator>> (istream &s, string &x )
- {
- char buf[INPUTBUFSIZE] ;
- s >> buf ; // might overflow
- x = buf ;
- cout << "echo : " << x << '\n' ;
- return s ;
- } // this is unsafe at present - so be careful
- #else
- void string::display()
- {
- printf("%s\n", p->s) ;
- } // display the string
- #endif // _KERNEL
-
- char &string::operator[] ( int i )
- {
- if ( i < 0 || strlen(p->s) < i )
- return (err_val) ;
- return ( p->s[i]) ;
- } // allows indexing into string array
-